package com.xjrsoft.module.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.YesOrNoEnum;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.erpModel.caseErpSupplier.dto.AddCaseErpSupplierDto;
import com.xjrsoft.module.organization.entity.Role;
import com.xjrsoft.module.organization.entity.UserRoleRelation;
import com.xjrsoft.module.organization.vo.UserRoleVo;
import com.xjrsoft.module.system.dto.*;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.entity.ExcelAuth;
import com.xjrsoft.module.system.entity.ExcelFile;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.service.IExcelAuthService;
import com.xjrsoft.module.system.service.IExcelFileService;
import com.xjrsoft.module.system.vo.ExcelFilePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 在线excel表 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2024-03-25
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/excel-file")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/excel-file", tags = "在线excel")
@AllArgsConstructor
public class ExcelFileController {

    private final IExcelFileService excelFileService;

    @GetMapping("/page")
    @ApiOperation(value = "查询文件列表(分页)")
    public R page(PageInput dto) {
        PageOutput<ExcelFilePageVo> pageList = excelFileService.getPageList(dto);
        return R.ok(pageList);
    }

    @PostMapping
    @ApiOperation(value = "新增excel")
    public R add(@Valid @RequestBody AddExcelDto dto) throws JsonProcessingException {
        ExcelFile excelFile = BeanUtil.toBean(dto, ExcelFile.class);
        return R.ok(excelFileService.save(excelFile));
    }

    @PutMapping("update-excel-name")
    @ApiOperation(value = "修改excelName")
    @Transactional(rollbackFor = Exception.class)
    public R updateExcelName(@Valid @RequestBody UpdateExcelNameDto dto) {
        ExcelFile excelFile = BeanUtil.toBean(dto, ExcelFile.class);
        return R.ok(excelFileService.updateById(excelFile));
    }

    @PutMapping("update-excel")
    @ApiOperation(value = "修改excel")
    @Transactional(rollbackFor = Exception.class)
    public R updateExcel(@Valid @RequestBody UpdateExcelDto dto) {
        ExcelFile excelFile = BeanUtil.toBean(dto, ExcelFile.class);
        return R.ok(excelFileService.updateById(excelFile));
    }

    @GetMapping("/excel-info")
    @ApiOperation(value = "获取文件信息")
    public R excelInfo(@RequestParam(required = true) Long id) {
        ExcelFile excelFile = excelFileService.getById(id);
        return R.ok(excelFile);
    }

    @DeleteMapping
    @ApiOperation(value = "删除excel文件信息")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody List<Long> ids){
        excelFileService.deleteExcel(ids);
        return R.ok();
    }

    @PutMapping("update-excel-auth")
    @ApiOperation(value = "修改excel权限")
    @Transactional(rollbackFor = Exception.class)
    public R updateExcelAuth(@Valid @RequestBody UpdateExcelAuthDto dto) {
        return R.ok(excelFileService.updateExcelAuth(dto));
    }

    @GetMapping("/excel-auth-info")
    @ApiOperation(value = "获取文件权限数据")
    public R excelAuthInfo(@RequestParam(required = true) Long id) {
        return R.ok(excelFileService.excelAuthInfo(id));
    }

}
